package com.my.mytest.activity;

import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.my.mytest.R;
import com.my.mytest.adapter.LayoutAdapter;
import com.my.mytest.util.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;

public class StaggeredGridLayoutActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ArrayList<Integer> mItems = new ArrayList<>(Arrays.asList(
//            R.drawable.ic_girls_1,
//            R.drawable.ic_girls_2,
//            R.drawable.ic_girls_3,
//            R.drawable.ic_girls_4,
//            R.drawable.ic_girls_5,
//            R.drawable.ic_girls_6,
//            R.drawable.ic_girls_7,
//            R.drawable.ic_girls_8,
//            R.drawable.ic_girls_9,
//            R.drawable.ic_girls_10
            R.mipmap.p1,
            R.mipmap.p2,
            R.mipmap.p3,
            R.mipmap.p4,
            R.mipmap.p5,
            R.mipmap.p6,
            R.mipmap.p7,
            R.mipmap.p8,
            R.mipmap.p9,
            R.mipmap.p10,
            R.mipmap.p11,
            R.mipmap.p12,
            R.mipmap.p13,
            R.mipmap.p14,
            R.mipmap.p15,
            R.mipmap.p16,
            R.mipmap.p17,
            R.mipmap.p18,
            R.mipmap.p19,
            R.mipmap.p20,
            R.mipmap.p21));

    private LayoutAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staggered_grid_layout);

        recyclerView = findViewById(R.id.list);

        StaggeredGridLayoutManager sglm = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(sglm);
        recyclerView.addItemDecoration(new SpaceItemDecoration(this, 64, 2));// 添加分割线

        runLayoutAnim(recyclerView, 1);

        adapter = new LayoutAdapter(this);

        recyclerView.setAdapter(adapter);
        adapter.setData(mItems);

    }

    private void runLayoutAnim(RecyclerView recyclerView, int flag) {
        final LayoutAnimationController controller;
        if (flag == 1) { // 由上到下
            controller =
                    AnimationUtils.loadLayoutAnimation(this, R.anim.layout_animation_fall_down);
        } else if (flag == 2) { // 从左往右
            controller =
                    AnimationUtils.loadLayoutAnimation(this, R.anim.layout_anim_left2right);
        } else {
            return;
        }


        recyclerView.setLayoutAnimation(controller);
        recyclerView.scheduleLayoutAnimation();
    }
}